import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {
  ColumnItem,
  Course,
  CourseGPA,
  CourseInfo,
  Student,
  StudentCCC,
  Studentcourse,
  studentGPA,
  Studentgrade
} from '../student';
import {HttpService} from '../http.service';
import {ActivatedRoute, Router} from '@angular/router';
import {EChartOption} from "echarts";


@Component({
  // tslint:disable-next-line:component-selector
  selector: 'gpa-major',
  templateUrl: './gpa-major.component.html',
  styleUrls: ['./gpa-major.component.css']
})

export class GPAMajorComponent {
  panel3 = [
    {
      active: false,
      disabled: false,
      name: '专业前10，后10',
    }
  ];
  panel4 = [
    {
      active: false,
      disabled: false,
      name: '各专业平均绩点',
    }
  ];
  constructor(private cdf: ChangeDetectorRef,
              public router: Router,
              private httpService: HttpService) {}
  studentGPA: studentGPA[] = [];
  students: Student[] = [];
  courseinfo: Course[] = [];
  courseData: CourseInfo[] = [];
  student: Student = {
    birthday: '',
    class: '',
    major: '',
    name: '',
    native_place: '',
    sex: '',
    stuid: null,
    time_of_enrollment: '',
  };
  courseid: string;
  private sumcredit: number;
  private sumGPA: number;
  private studentsData: Student[];
  private courseDataPlay: CourseInfo[];
  private courseinfoData: Course[];
  private studentGPAData: studentGPA[];
  private people: number;
  private GPA: number;
  AveGPA: number;
  loading = true;

  listOfColumns: ColumnItem[] = [
    {
      name: '学号',
      sortOrder: null,
      sortFn: (a: Student, b: Student) => a.stuid - b.stuid,
    },
    {
      name: '姓名',
    },
    {
      name: '专业',
      filterMultiple: false,
      listOfFilter: [
        { text: '信息管理与信息系统', value: '信息管理与信息系统' },
        { text: '市场营销学', value: '市场营销学' },
        { text: '国际经济与贸易', value: '国际经济与贸易' },
        { text: '金融学', value: '金融学' },
        { text: '会计', value: '会计' },
      ],
      filterFn: (major: string, item: Student) => item.major.indexOf(major) !== -1
    },
    {
      name: '班级',
    },
    {
      name: '总学分',
      sortOrder: null,
      sortFn: (a: studentGPA, b: studentGPA) => a.totalCredits - b.totalCredits,
    },
    {
      name: '绩点',
      sortOrder: 'descend',
      sortFn: (a: studentGPA, b: studentGPA) => a.GPA - b.GPA,
    }
  ];
  MajorGPAData = [0,0,0,0,0];
  MajorData = ['市场营销学', '国际经济与贸易','信息管理与信息系统', '金融学','会计学'];
  option : EChartOption = {};
  getStudents(): void {
    this.httpService.getStudents().subscribe(
      (res) => {
        res.data.forEach(row => {
          this.students = [...this.students];
          this.students.push( {
            native_place: row.native_place,
            time_of_enrollment: row.time_of_enrollment,
            birthday : row.birthday,
            class : row.class,
            major : row.major,
            name : row.name,
            sex : row.sex,
            stuid : row.stuid
          });
        });
        this.studentsData = this.students;
        console.log(this.students);
      });
  }
  getcouresinfo() {
    this.httpService.course_select1().subscribe(
      res => {
        res.data.forEach(cou => {
          this.courseinfo = [...this.courseinfo];
          this.courseinfo.push({
            courseid: cou.courseid,
            score: cou.score,
            stuid: cou.stuid,
          });
        });
        console.log(this.courseinfo);
        this.courseinfoData = this.courseinfo;
      });
  }
  getCourseData() {
    this.httpService.getCourses().subscribe(
      res => {
        res.data.forEach(cou => {
          this.courseData = [...this.courseData];
          this.courseData.push({
            courseid: cou.courseid,
            coursename: cou.coursename,
            credit: cou.credit,
            type: cou.type
          });
        });
        console.log(this.courseData);
        this.courseDataPlay = this.courseData;
      });
  }
  getTotalGPA(stuid) {
    for (const i in this.studentsData) {
      if (stuid === this.studentsData[i].stuid) {
        this.sumcredit = 0;
        this.sumGPA = 0;
        for ( const j in this.courseinfoData) {
          if (this.studentsData[i].stuid === this.courseinfoData[j].stuid) {
            for (const k in this.courseDataPlay) {
              if (this.courseinfoData[j].courseid === this.courseDataPlay[k].courseid) {
                this.sumcredit += this.courseDataPlay[k].credit;
                this.sumGPA += this.courseDataPlay[k].credit * (this.courseinfoData[j].score / 10 - 5);
                break;
              }
            }
          }
        }
        return this.sumGPA / this.sumcredit;
        break;
      }
    }
  }
  getTotalCredit(stuid) {
    for (const i in this.studentsData) {
      if (stuid === this.studentsData[i].stuid) {
        this.sumcredit = 0;
        this.sumGPA = 0;
        for ( const j in this.courseinfoData) {
          if (this.studentsData[i].stuid === this.courseinfoData[j].stuid) {
            for (const k in this.courseDataPlay) {
              if (this.courseinfoData[j].courseid === this.courseDataPlay[k].courseid) {
                this.sumcredit += this.courseDataPlay[k].credit;
                this.sumGPA += this.courseDataPlay[k].credit * (this.courseinfoData[j].score / 10 - 5);
                break;
              }
            }
          }
        }
        return this.sumcredit;
        break;
      }
    }
  }
  getStudentGPA() {
    this.httpService.getStudents().subscribe(
      (res) => {
        res.data.forEach(row => {
          this.studentGPA = [...this.studentGPA];
          // @ts-ignore
          this.studentGPA.push( {
            stuid : row.stuid,
            totalCredits : this.getTotalCredit(row.stuid),
            class : row.class,
            major : row.major,
            name : row.name,
            GPA : this.getTotalGPA(row.stuid),
          });
          this.loading = false;
        });
        console.log(this.studentGPA);
        this.studentGPAData = this.studentGPA;
        this.getMajorGPA(this.studentGPA);
        this.GPA = 0;
        for (const i in this.studentGPA) {
          this.GPA  += this.studentGPA[i].GPA;
        }
        this.AveGPA = this.GPA / this.studentGPA.length;
        console.log(this.AveGPA)
        this.option = {
          title: {
            text: '专业平均绩点',
            subtext: '学院平均绩点'+ this.AveGPA.toFixed(4),
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['最高', '最低']
          },
          toolbox: {
            show: true,
            feature: {
              dataZoom: {
                yAxisIndex: 'none'
              },
              dataView: {readOnly: false},
              magicType: {type: ['line', 'bar']},
              restore: {},
              saveAsImage: {}
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.MajorData
          },
          yAxis: {
            min:2.4,
            max:2.6,
            type: 'value',
            axisLabel: {
              formatter: '{value} °C'
            }
          },
          series: [
            {
              name: '专业平均绩点',
              type: 'line',
              data: this.MajorGPAData,
              markPoint: {
                data: [
                  {type: 'max', name: '最大值'},
                  {type: 'min', name: '最小值'}
                ]
              },
              markLine: {
                data: [
                  {type: 'average', name: '平均值'}
                ]
              }
            },
          ]
        };
      });
  }
  getMajorGPA(studentGPAData) {
    for (const i in this.MajorData) {
      this.people = 0;
      this.GPA = 0;
      for (const j in studentGPAData) {
        if (this.MajorData[i]=== studentGPAData[j].major) {
          this.GPA += studentGPAData[j].GPA;
          this.people += 1;
        }
        this.MajorGPAData[i] = Number((this.GPA / this.people).toFixed(4));
      }
    }
    console.log(this.MajorGPAData);
  }
  // tslint:disable-next-line:use-lifecycle-interface
  ngOnInit(): void {
    this.getStudents();
    this.getcouresinfo();
    this.getCourseData();
    this.getStudentGPA();
  }
}
